package com.ley.springboot.commons.utils;

import java.util.Random;

/**
 * random utils and this random utility class add random number string and
 * random string
 *
 * @see org.apache.commons.lang3.RandomUtils
 **/
public class RandomUtils extends org.apache.commons.lang3.RandomUtils {

    /**
     * number list
     **/
    private static final char[] NUMBER_LIST = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    /**
     * code sequence
     **/
    private static final char[] CODE_SEQUENCE = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '8', '9'};

    /**
     * Random object used by random method. This has to be not local to the
     * random method so as to not return the same value in the same millisecond.
     */
    private static final Random RANDOM = new Random();


    /**
     * get length random string
     *
     * @param length the length of the generate random string
     * @return the generate random string
     **/
    public static final String randomString(int length) {
        if (length < 0) {
            throw new IllegalArgumentException("length need >= 0");
        } else {
            StringBuilder randomStringBuilder = new StringBuilder();
            for (int i = 0; i < length; ++i) {
                randomStringBuilder.append(String.valueOf(CODE_SEQUENCE[RANDOM.nextInt(CODE_SEQUENCE.length)]));
            }
            return randomStringBuilder.toString();
        }
    }


    /**
     * get length number string
     *
     * @param length the length of the generate random number string
     * @return the generate random number string
     **/
    public static final String randomNumberString(int length) {
        if (length < 0) {
            throw new IllegalArgumentException("length need >= 0");
        } else {
            StringBuilder numberStringBuilder = new StringBuilder();
            for (int i = 0; i < length; ++i) {
                numberStringBuilder.append(String.valueOf(NUMBER_LIST[RANDOM.nextInt(NUMBER_LIST.length)]));
            }

            return numberStringBuilder.toString();
        }
    }
}
